<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="编辑员工">
    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">
    <style type="text/css">
        .layui-form-item .layui-word-aux {
            font-size: 12px;
            color: #FF5722;
        }

        .readonly {
            background-color: #eeeeee;
        }
    </style>
    <script>
        function closeSelf() {
            closeSubIframe();
        }
        jQuery(function () {
            $("#close").click(function () {
                closeSelf();
            });
            
            layui.use(['form', 'layedit', 'laytpl','element'], function(){
                var form = layui.form
                    ,layer = layui.layer
                    ,layedit = layui.layedit
                    ,laytpl = layui.laytpl;

                var ajax1= get("${baseUrl}organization/store/getStores.action",function (resp) {
                    if (resp.code == 1) {
                        var $select = $("[name='storeId']");
                        $select.empty();
                        $.each(resp.data,function (i,data) {
                            $select.append("<option value='"+data.id+"'>"+data.name+"</option>");
                        });
                    }
                });

                var ajax2= get("${baseUrl}user/role/listWithoutAdmin.action",function (resp) {
                    if (resp.code == 1) {
                        laytpl(role.innerHTML).render(resp.data, function(html){
                            $("#roleDiv").empty().append(html);
                            formRender();
                        });
                    }
                });


                var ajax3= get("${baseUrl}organization/house/listNormalVo.action",function (resp) {
                    if (resp.code == 1) {
                        laytpl(linkHouseTable.innerHTML).render(resp.data, function(html){
                            $("#userHouseDiv").empty().append(html);
                            formRender();
                        });
                    }
                });

                $.when(ajax1,ajax2,ajax3).done(function () {
                    get("${baseUrl}user/user/getVoById.action?id=${id}", function (resp) {
                        if (resp.code == 1) {
                            var data = resp.data;
                            var json={
                                "id":data.id
                                ,"username":data.username
                                ,"name":data.name
                                ,"sex": data.sex
                                ,"storeId": data.storeId
                                ,"viewOtherHouse": data.viewOtherHouse
                                ,"viewOtherOrder": data.viewOtherOrder
                                ,"cancelOtherOrder": data.cancelOtherOrder
                                ,"viewOtherCustom": data.viewOtherCustom
                                ,"phone": data.phone
                                ,"email": data.email
                                ,"address": data.address
                                ,"status": data.status
                            };

                            $.each(data.houseList,function (i,d) {
                                json['house['+d.id+']'] = true;
                            });


                            $.each(data.roleList,function (i,d) {
                                json['role['+d.id+']'] = true;
                            });

                            $.each(data.price.split("|"), function(i,priceCode){
                                json['price['+priceCode+']'] = true;
                            });

                            formSetVal('userInfo', json)
                        }
                    });
                });


                layer.load();
                get("${baseUrl}user/getAdmin",function (res) {
                    layer.closeAll();
                    if (res.code == 1){
                        if (res.data.id == "${userVo.id}") {
                            $(".adminNoShow").hide();
                        }
                    }
                });

                form.on('checkbox(viewOtherHouse)', function (data) {
                    if (!data.elem.checked) {
                        $('.linkHouse').next().removeClass("layui-form-checked");
                    } else {
                        $('[storeId="'+_login_user.storeId+'"]').next().addClass("layui-form-checked");
                    }
                });

                //监听提交
                form.on('submit(save)', function(data){
                    console.log(JSON.stringify(data.field));
                    saveData(data);
                    return false;
                });
            });
        });

        function saveData(data) {
            var json={};
            var userRoleList=[];
            var userHouseList=[];
            var price="";
            tipLoad("编辑中,请稍后...");
            for (var key in data.field) {
                var value = data.field[key];
                if (key.indexOf("role") != -1) {
                    //角色id
                    var roleId = key.replace("role[","").replace("]","");
                    var role = {};
                    role["id"] = roleId;
                    userRoleList.push({roleId:roleId});
                } else if(key.indexOf("house") != -1) {
                    //仓库id
                    var houseId = key.replace("house[","").replace("]","");
                    var house = {};
                    house["id"] = houseId;
                    userHouseList.push({houseId:houseId});
                } else if(key.indexOf("price") != -1) {
                    //权限价格
                    var temp = key.replace("price[","").replace("]","");
                    price += temp+"|";
                } else {
                    json[key] =value;
                }
            }
            json['userRoleList'] = userRoleList;
            json['userHouseList'] = userHouseList;
            json['price'] = price.substring(0,price.length-1);
            json.cancelOtherOrder = isTrue(data.field.cancelOtherOrder);
            json.viewOtherOrder = isTrue(data.field.viewOtherOrder);
            json.viewOtherCustom = isTrue(data.field.viewOtherCustom);
            json.viewOtherHouse = isTrue(data.field.viewOtherHouse);
            json.transferOtherHouse = isTrue(data.field.transferOtherHouse);

            console.log("转化后:"+JSON.stringify(json));
            post("${baseUrl}user/user/update.action",json,function (resp) {
                if (resp.code == 1){
                    refreshMainIframe("${baseUrl}user/user.action");
                    closeAll();
                    tip('更新成功');
                    setTimeout(function () {
                        closeSelf();
                    },500);
                } else {
                    closeAll();
                    errTip(resp.msg);
                }
            });
        }
    </script>
</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" method="post" action="" lay-filter="userInfo">
            <div style="padding: 14px 0 0 4px;font-size: 16px;">基本信息</div>
            <div class="horizon-line" style="margin: 4px 0 4px 0"></div>

            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" autocomplete="off"  readonly="readonly" class="layui-input readonly">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">员工姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入员工姓名" autocomplete="off"  class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" title="男" checked>
                    <input type="radio" name="sex" value="2" title="女">
                </div>
            </div>


        <div class="adminNoShow">
            <div style="padding: 14px 0 0 4px;font-size: 16px;">权限管理</div>
            <div class="horizon-line" style="margin: 4px 0 4px 0"></div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属门店</label>
                <div class="layui-input-inline">
                    <select name="storeId" lay-verify="required">
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div id="roleDiv" class="layui-input-block">
                </div>
            </div>

            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="checkbox" name="viewOtherHouse" lay-filter="viewOtherHouse" title="查看其他门店" value="true" lay-skin="primary">
                </div>
                <div class="layui-form-mid layui-word-aux">勾选后，可以查看或选择其他门店的仓库</div>
            </div>

            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label">关联仓库</label>
                <div id="userHouseDiv" class="layui-input-block">
                </div>
                <div class="layui-form-mid layui-word-aux">关联仓库后，可以查看仓库库存以及经营仓库业务。
                    （其中，分店员工关联其他门店仓库只能查看其他门店仓库库存，不能经营其他门店业务。）</div>
            </div>

            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label">价格权限</label>
                <div class="layui-input-block">
                    <%--1零售价 2批发价 3参考进货价 4最低售价 5成本价 6等级价--%>
                    <input type="checkbox" name="price[1]" title="零售价" lay-skin="primary">
                    <input type="checkbox" name="price[2]" title="批发价" lay-skin="primary">
                    <input type="checkbox" name="price[3]" title="参考进货价" lay-skin="primary">
                        <input type="checkbox" name="price[4]" title="最低售价" lay-skin="primary">
                    <input type="checkbox" name="price[5]" title="成本价" lay-skin="primary">
                    <input type="checkbox" name="price[6]" title="等级价" lay-skin="primary">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">其他</label>
                <div class="layui-input-inline">
                    <input type="checkbox" name="viewOtherOrder" title="查看他人单据" value="true" lay-skin="primary">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    勾选后，可以查看他人经手的单据。分店仅限查看本门店的单据，总部可以查看所有人单据。
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="checkbox" name="cancelOtherOrder" title="作废他人单据" value="true" lay-skin="primary">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    勾选后，可以作废他人经手的单据。分店仅限作废本门店的单据，总部可以作废所有人单据。
                </div>
            </div>

            <div class="layui-form-item" style="display: none;">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="checkbox" name="viewOtherCustom" title="查看他人客户" value="true" lay-skin="primary">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    勾选后，可以查看关联他人的客户和客户应收欠款，开单时也可以选择关联他人的客户。
                </div>
            </div>
        </div>

            <div style="padding: 14px 0 0 4px;font-size: 16px;">更多</div>
            <div class="horizon-line" style="margin: 4px 0 4px 0"></div>
            <div class="layui-form-item">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                    <input type="text" name="email" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" autocomplete="off" placeholder="请输入地址" class="layui-input">
                </div>
            </div>


            <div class="adminNoShow">
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="status" value="1" title="正常" checked>
                        <input type="radio" name="status" value="2" title="锁定">
                    </div>
                </div>
            </div>

            <div class="horizon-line" style="margin: 4px 0 4px 0"></div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="save">提交</button>
                    <a id="close" class="layui-btn layui-btn-primary" href="javascript:;">
                        取消
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script id="linkHouseTable" type="text/html">
    {{#  layui.each(d, function(i, houseVo){ }}

    {{#  if(houseVo.storeId == _login_user.storeId){ }}
    <input type='checkbox' class="linkHouse" name='house[{{houseVo.id}}]' title='{{houseVo.store.name}}-{{houseVo.name}}' lay-skin='primary' storeId='{{houseVo.storeId}}' checked>
    {{#  } else { }}
    <input type='checkbox' class="linkHouse" name='house[{{houseVo.id}}]' title='{{houseVo.store.name}}-{{houseVo.name}}' lay-skin='primary' storeId='{{houseVo.storeId}}'>
    {{#  } }}

    {{#  }); }}
</script>

<script id="role" type="text/html">
    {{#  layui.each(d, function(i, data){ }}
    <input type='checkbox' name='role[{{data.id}}]' title='{{data.name}}' lay-skin='primary'>
    {{#  }); }}
</script>
<html:loading/>
</html>